package com.diqizu.servlet.admin;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.diqizu.dao.LunBoDaoImpl;
import com.diqizu.entity.GongSi;
import com.diqizu.entity.LunBo;
import com.diqizu.servlet.BaseServlet;
import com.diqizu.utli.Result;

/**
 * Servlet implementation class bannerservletAdmian
 */
@WebServlet("/admin/BannerServletAdmian")
public class BannerServletAdmian extends BaseServlet {
	LunBoDaoImpl lb = new LunBoDaoImpl();

	public void getList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 1.盗用dao层方法的方法获取数据
		ArrayList<LunBo> allList = lb.getALllb();
		// 2.将数据封装为layu需要的格式
		String res = Result.toClient(0, "轮播图列表", allList, allList.size());
		PrintWriter out = resp.getWriter();
		out.write(res);
		out.flush();
		out.close();
	}

	public void insertlunbo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String imageName = req.getParameter("imageName");
		String imag = req.getParameter("imag");
		String paixu = req.getParameter("paixu");
		String dizhi = req.getParameter("dizhi");
		LunBo banner = new LunBo(0, imageName, imag,paixu,dizhi);
		boolean res = lb.getALllb2(banner);
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "添加成功"));
		} else {
			out.write(Result.toClient(1, "添加失败"));
		}
		out.flush();
		out.close();
	}

	public void updatelunbo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String imageName = req.getParameter("imageName");
		String imag = req.getParameter("imag");
		String imagId = req.getParameter("imagId");
		String paixu = req.getParameter("paixu");
		String dizhi = req.getParameter("dizhi");
		LunBo banner = new LunBo(Integer.valueOf(imagId), imageName, imag,dizhi,paixu);
		boolean res = lb.getALllb3(banner);
		PrintWriter out = resp.getWriter();
		if (res) {
			out.write(Result.toClient(0, "修改成功"));
		} else {
			out.write(Result.toClient(1, "修改失败"));
		}

		out.flush();
		out.close();
	}

	public void deletlunbo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 从页面上拿到数据
		String imagId = req.getParameter("imagId");
		int Id = Integer.valueOf(imagId);
		// 调用dao层方法
		boolean res = lb.getALllb4(Id);

		PrintWriter out = resp.getWriter();
		String resJson = "";
		if (res) {
			resJson = Result.toClient(0, "删除成功");
		} else {
			resJson = Result.toClient(1, "删除成功");
		}
		out.write(resJson);
		out.flush();
		out.close();
	}
}
